package com.cbw.view;

import com.cbw.bean.Book;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

public class Views {
    private Scanner sc = new Scanner(System.in);

    public void welcome() {
        print("欢迎使用图书管理系统！");
    }

    private void print(String msg) {
        System.out.println(msg);
    }

    private String inputStr(String msg) {
        print(msg);
        return sc.nextLine();
    }

    public void printMainMenu() {
        print("1.登录，2.退出程序");
    }

    public int inputChoose(int max) {
        String str = inputStr("请输入一个大于0而且小于" + max + "的数字：");
        int num = -1;
        try {
            num = Integer.parseInt(str);
        } catch (NumberFormatException e) {

        }
        if (num < 0 || num > max) {
            num = inputChoose(max);
        }
        return num;
    }

    public void bye() {
        print("谢谢使用，再见！");
    }

    public String inputUsername() {
        return inputStr("请输入用户名：");
    }

    public String inputPassword() {
        return inputStr("请输入密码");
    }

    public void loginFail() {
        print("登录失败!");
    }

    public void loginSuccess() {
        print("登录成功！");
    }

    public void printAdminMenu() {
        print("1.增加图书，2.删除图书，3.修改图书，4.查询图书，5.返回主菜单");
    }

    public void backMain() {
        print("返回主菜单");
    }

    public void printShowMenu() {
        print("1.模糊查询，2.价格从高到低排序显示，3.价格从低到高排序显示，4.出版日期排序，5.返回管理员菜单");
    }

    public void backAdmin() {
        print("返回管理员菜单");
    }

    public String inputBookName() {
        return inputStr("请输入书籍名称：");
    }

    public double inputBookPrice() {
        return inputBookPrice("请输入书籍的价格：");
    }

    public double inputNewBookPrice() {
        return inputBookPrice("请输入新的书籍价格：");
    }

    public double inputBookPrice(String msg) {
        String str = inputStr(msg);
        double num = -1;
        try {
            num = Double.parseDouble(str);
        } catch (NumberFormatException e) {

        }
        if (num < 0) {
            num = inputBookPrice();
        }
        DecimalFormat df = new DecimalFormat("#.00");
        String format = df.format(num);
        return Double.parseDouble(format);
    }

    //""
    public String inputBookPublishDate(String msg) {
        String str = inputStr(msg);
        Date date = null;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = df.parse(str);
        } catch (ParseException e) {

        }
        if (date == null) {
            str = inputBookPublishDate(msg);
        }
        return str;

    }

    public String inputNewBookDate() {
        return inputBookPublishDate("请输入新的出版日期：日期格式：yyyy-MM-dd");
    }

    public String inputBookDate() {
        return inputBookPublishDate("请输入出版日期：日期格式：yyyy-MM-dd");
    }

    public void printAddResult(boolean add) {
        print(add ? "添加成功！" : "添加失败!");
    }

    public void printRemoveResult(boolean remove) {
        print(remove ? "删除成功！" : "删除失败!");
    }

    public String inputNewBookName() {
        return inputStr("请输入新的书籍名称：");
    }


    public void printModifyResult(boolean modify) {
        print(modify ? "修改成功！" : "修改失败!");
    }

    public void showBooks(ArrayList<Book> list) {
        for (Book book : list) {
            print(book.getBookName() + "\t" + book.getBookPrice() + "\t" + book.getBookPublishDate());
        }
    }
}
